package com.rt.tmpt.others.quartz2.manager;

import org.quartz.*;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class JobManager {  
  
      
    /* 当初我初始化的是  SchedulerFactoryBean schedulerFactoryBean；  这样是注入不进去的 报下面的错 
       nested exception is org.springframework.bean.factory.BeanNotOfRequiredTypeException:
       Bean named 'schedulerFactoryBean' must be of  
       type [org.springframework.scheduling.quartz.SchedulerFactoryBean],  
       but was actually of type [org.quartz.impl.StdScheduler>] 
       	看spring源码可以知道，其实spring得到的是一个工厂bean，得到的不是它本身，而是它负责创建的org.quartz.impl.StdScheduler对象            所以要使用Scheduler对象 
    */  
    @Resource  
    private Scheduler scheduler ;  
      
  
    /** 
     * 添加一个定时任务 
     */  
    public  void addJob(AutoJobDTO job, Class classz) throws SchedulerException  {
        //这里获取任务信息数据  
        TriggerKey triggerKey = TriggerKey.triggerKey(job.getJob_name(), job.getJob_group());  
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);  
        if(trigger==null){  
            System.out.println("trigger is null----");  
            //不存在，创建一个  
            JobDetail jobDetail = JobBuilder.newJob(classz).withIdentity(job.getJob_name(), job.getJob_group()).build();  
            jobDetail.getJobDataMap().put("scheduleJob", job);  
            //表达式调度构建器  
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getJob_time());  
            //按新的cronExpression表达式构建一个新的trigger  
            trigger = TriggerBuilder.newTrigger().withIdentity(job.getJob_name(), job.getJob_group()).withSchedule(scheduleBuilder).build();  
            scheduler.scheduleJob(jobDetail,trigger);  
        }else{  
            // Trigger已存在，那么更新相应的定时设置  
            //表达式调度构建器  
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getJob_time());  
            //按新的cronExpression表达式重新构建trigger  
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();  
            //按新的trigger重新设置job执行  
            scheduler.rescheduleJob(triggerKey, trigger);  
        }  
    }  
    /** 
     * 启动所有定时任务 
     */  
    public  void startJobs() {  
        try {  
            scheduler.start();  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        }  
    }  
    
    /**
     * Remove a Job
     * job.getJob_name()
     * job.getJob_group()
     * @param job
     */
    public void removeJob(AutoJobDTO job) {
        try {  
        	TriggerKey triggerKey = TriggerKey.triggerKey(job.getJob_name(), job.getJob_group());  
        	JobKey jobKey = JobKey.jobKey(job.getJob_name(), job.getJob_group());
        	
        	scheduler.pauseJob(jobKey);// 停止触发器  
        	scheduler.unscheduleJob(triggerKey);// 移除触发器  
        	scheduler.deleteJob(jobKey);// 删除任务  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  
    

    
}  